﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace STSdb.MTL
{
    public class TableCompactingState
    {
        /***************************************************   DONT FORGET ADDING TO EXHALE AND SETTING OF THIS STATE MUST BE DONE IN ONE TRANSACTION   *********************************/

        /// <summary>
        /// Maximum size of the information about compacting file
        /// </summary>
        public static int _CompactingStateSize = 64;

        public TableCompactingState()
        {
            CompactingState = eState.STOP;
            Table0handle = 0;
            Table1handle = 0;
            //SrcTN = String.Empty;
            //DstTN = String.Empty;

            ActiveTable = 0;

            LastCompactionWasMade = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        }


        public enum eState
        {
            STOP = 0,
            //DESTINATION_TABLE_IS_CREATING = 1,
            DESTINATION_TABLE_IS_CREATED = 1,
            DATA_IS_COPIED = 2,
            SOURCE_TABLE_IS_DELETED = 3
        }

        /// <summary>
        /// Either 0 or 1, 0 always corresponds to the original table name locator, 1 to its compacted name @sys_etn_userTableName/1
        /// </summary>
        public int ActiveTable { get; set; }


        public eState CompactingState { get; set; }

        /// <summary>
        /// Source Table Raw file handle
        /// </summary>
        public ulong Table0handle { get; set; }

        /// <summary>
        /// Destination table Raw file handle
        /// </summary>
        public ulong Table1handle { get; set; }

        /// <summary>
        /// Default value is 1970 year, indicates last time when compaction was made
        /// </summary>
        public DateTime LastCompactionWasMade { get; set; }

        ///// <summary>
        ///// Indicates UTC DateTime in ticks when last compaction was made
        ///// </summary>
        //public long LastCompactionWasMade { get; set; }

        // <summary>
        // Source table name
        // </summary>
        //public string SrcTN { get; set; }

        // <summary>
        // Destination table name
        // </summary>
        //public string DstTN { get; set; }






        public static byte[] GetStateAsByteArray(TableCompactingState tcs)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(tcs.ActiveTable.ToString());       //Which is active original name or compacted name: 0 or 1
            sb.Append(";");
            sb.Append(Convert.ToInt32(tcs.CompactingState).ToString());      //Compacting state
            sb.Append(";");
            sb.Append(tcs.Table0handle.ToString());         //Table 0 raw file handle
            sb.Append(";");
            sb.Append(tcs.Table1handle.ToString());         //Table 1 raw file handle
            sb.Append(";");
            sb.Append(tcs.LastCompactionWasMade.Ticks.ToString());         //Time when compaction write was made
            sb.Append(";");



            //1 byte - active (first or second);"first table name"; "first file handle" ;"second table name"; "second file handle"

            byte[] rbt = new byte[TableCompactingState._CompactingStateSize];
            int i = 0;
            foreach (byte bt in System.Text.Encoding.ASCII.GetBytes(sb.ToString()))
            {
                rbt[i] = bt;
                i++;
            }

            return rbt;

        }

        public static TableCompactingState GetTableCompactingStateFromByteArray(byte[] bt)
        {
            //Max size can't exceed 64 bytes _CompactingStateSize

            TableCompactingState tcs = new TableCompactingState();

            string[] el = System.Text.Encoding.ASCII.GetString(bt).Split(new string[] { ";" }, StringSplitOptions.None);

            tcs.ActiveTable = Convert.ToInt32(el[0]);
            tcs.CompactingState = (eState)Convert.ToInt32(el[1]);
            tcs.Table0handle = Convert.ToUInt64(el[2]);
            tcs.Table1handle = Convert.ToUInt64(el[3]);

            //From here we start supporting

            if (el.Length > 4)     //Starting support of prev versions
                tcs.LastCompactionWasMade = new DateTime(Convert.ToInt64(el[4]));

            return tcs;
        }





    }
}
